package com.example.sunmiv1.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.OrderDetailInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DeliveryGoodsAdapter extends BaseAdapter {

    private List<OrderDetailInfo.DataBean> list;
    private ViewHolder viewHolder;

    public DeliveryGoodsAdapter(List<OrderDetailInfo.DataBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.delivery_goods_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        OrderDetailInfo.DataBean bean = list.get(position);
        viewHolder.deliveryGoodsName.setText(bean.getGoods_name());
        viewHolder.deliveryGoodsSku.setText(bean.getSku_name());
        viewHolder.deliveryGoodsNum.setText(bean.getNum() + "");
        if ("0".equals(bean.getSerial_number_state())) {
            viewHolder.deliveryGoodsView.setVisibility(View.GONE);
            viewHolder.deliveryGoodsChooseLin.setVisibility(View.GONE);
        } else {
            viewHolder.deliveryGoodsView.setVisibility(View.VISIBLE);
            viewHolder.deliveryGoodsChooseLin.setVisibility(View.VISIBLE);
            if (bean.getSerial_list().size() > 0) {
                viewHolder.deliveryGoodsChooseTv.setText("已選擇");
                viewHolder.deliveryGoodsChooseTv.setTextColor(ContextCompat.getColor(parent.getContext(), R.color.login_textColor1));
            } else {
                viewHolder.deliveryGoodsChooseTv.setText("未選擇");
                viewHolder.deliveryGoodsChooseTv.setTextColor(ContextCompat.getColor(parent.getContext(), R.color.text_666));
            }
        }
        final int index = position;
        viewHolder.deliveryGoodsChoose.setOnClickListener(v -> {
            choose.choose(index);
        });
        return convertView;
    }

    private Choose choose;

    public void setChoose(Choose choose) {
        this.choose = choose;
    }

    public interface Choose {
        void choose(int index);
    }

    static class ViewHolder {
        @BindView(R.id.deliveryGoods_name)
        TextView deliveryGoodsName;
        @BindView(R.id.deliveryGoods_sku)
        TextView deliveryGoodsSku;
        @BindView(R.id.deliveryGoods_num)
        TextView deliveryGoodsNum;
        @BindView(R.id.deliveryGoods_view)
        View deliveryGoodsView;
        @BindView(R.id.deliveryGoods_chooseTv)
        TextView deliveryGoodsChooseTv;
        @BindView(R.id.deliveryGoods_choose)
        TextView deliveryGoodsChoose;
        @BindView(R.id.deliveryGoods_chooseLin)
        LinearLayout deliveryGoodsChooseLin;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
